<?php
/*
 * share_collocation（注意分库分表）逻辑：
 * 	1、未通过的，记下标记，质检后直接删除（连并对应的share_goods一并删除）
 * 	2、审核通过，但未被推荐，则保留
 * 	3、审核通过，并被推荐，则copy一份至collocation
 * 对于审核通过的，share_collocation要长时间保存，该表是从用户主页中查看分享搭配的数据源
 * 
 * share_goods（注意分库分表）逻辑：
 * 	1、单品逻辑：
 * 		1.1、未通过，记下标记，质检后直接删除
 * 		1.2、审核通过，但未被推荐，则保留
 * 		1.3、审核通过，并被推荐，则copy一份至goods
 * 	2、搭配产品逻辑：
 * 		2.1、未通过，直接删除
 * 		2.2、审核通过，但未被推荐，则保留
 * 		2.3、审核通过，并被推荐，则copy一份至goods
 * 对于审核通过的，share_goods要长时间保存，该表是从用户主页中查看分享商品的数据源
 */
class ManagerController extends AppController
{
	
	var $autoRender = true;
    var $uses = array( 'Goods','Collocation','Share_goods','Share_collocation','Letter','Groups','Contacter','Class_path' );
    var $layout = 'manager';
    public $helpers;
    private $com_ctl;
    function init()
    {
    	$_SESSION['user_id'] = 111;
    	global $manager_list;
    	$this->helpers = array( 'html' );
    	$this->com_ctl = new Com_ctl();
    	/*if( !in_array( $_SESSION['user_id'], $manager_list ) )
    	{
    		exit;
    	}*/
    }
    function index( $audit_type )
    {
    	if( !isset( $audit_type ) )
    	{
    		$audit_type = 1;
    	}
    	$this->set( 'audit_type', $audit_type );
    }
    function get_group()
    {
    	$this->layout = 'empty';
    	$user_id = $_SESSION['user_id'];
    	$result = array();
    	$curr_time = date( 'Y-m-d H:i:s' );
    	//分配一单新的或个人名下的单据（未审核完毕，关闭页面所致）
    	$ret = $this->Groups->findAll( 'status='.NEW_GROUP.' or status='.DOING_GROUP.' and auditor='.$user_id, null, null, 1 );
    	if( count( $ret ) > 0 )
    	{
    		$ret[0]['Groups']['apply_type'] = $ret[0]['Groups']['apply_type']==0?'公开':'不公开';
    		$result = $ret[0]['Groups'];
    		$update_data = array( 
    			'id' => $ret[0]['Groups']['id'],
    			'status' => DOING_GROUP,//审核中
    			'auditor' => $user_id,
    			'update_time' => $curr_time
    		);
    		$this->Groups->save( $update_data );
    	}
    	$this->set( 'result', $result );
    	$this->render( 'group_index' ); 
    }
    function audit_group()//审核组信息
    {
    	$ret = 0;
    	$msg = '';
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$user_id = $_SESSION['user_id'];
    	$operate = init_web_para( 'operate', '' );//ok,通过审核，no,审核不通过
    	$id = init_web_para( 'id', '' );
    	$ret_group = $this->Groups->findAll( array( 'id' => $id ),null,null,1 );
    	if( count( $ret_group ) > 0 )
    	{
    		$temp = $ret_group[0]['Groups'];	
    		if( in_array( $temp['status'], array( NEW_GROUP, DOING_GROUP ) ) )
    		{
	    		$update_data = array(
		    		'id' => $id,
		    		'auditor' => $user_id,
		    		'update_time' => $curr_time
		    	);
		    	if( $operate == 'ok' || $operate == 'no' )
		    	{
		    		if( $operate == 'ok' )//通过审核
		    		{
		    			$update_data['status'] = AUDIT_GROUP;	
		    			$this->com_ctl->set_score( $temp['applicant'], TYPE_GROUP_SCORE );
		    			$content = '恭喜您，您申请的小组（'.$temp['name'].'）审核已通过'; 
		    		}
		    		else ////未通过审核
		    		{
		    			$update_data['status'] = FAIL_GROUP;
		    			$content = '很遗憾，您申请的小组（'.$temp['name'].'）审核未通过，原因如下：<br>'.filter_content( init_web_para( 'content', '' ) );
		    		}  
		    		$this->send_letter($content, $temp['applicant'] );
		    		$this->Groups->save( $update_data );					
		    	}
		    	else //无效操作
		    	{
		    		$ret = 1;
		    		$msg = '无效操作';
		    		$this->Groups->save( array( 'id' => $id,'status' => NEW_GROUP ) );//还原状态
		    	}
    		}
    		else 
    		{
    			$ret = 1;
	    		$msg = '该组信息已被他人审核';
    		}
    	}
    	else
	    {
	    	$ret = 1;
	    	$msg = '无对应的待审组信息';
	    }
    	echo json_encode( array( 'ret' => $ret, 'msg' => $msg ) );
    	exit;
    }
    function get_unsyn_goods()
	{
    	$this->layout = 'empty';
    	$result = array();
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$where = array( 'status' => NEW_GOODS );
    	$ret = $this->Share_goods->findAll( $where, null, null, 1 );
    	if( count( $ret ) > 0 )
    	{
    		$result = $ret[0]['Share_goods'];
    	}
    	$this->set( 'result', $result );
    	$this->render( 'syn_goods_index' );
    }
    function syn_goods()
    {
    	$ret = 0;
    	$msg = '';
    	$id = init_web_para( 'id', 0 );
    	$mark = init_web_para( 'mark', '' );
    	$t_url = init_web_para( 't_url', '' );
    	$img_url = init_web_para( 'img_url', '' );
    	$brokerage = init_web_para( 'brokerage', 0 );
    	$price = init_web_para( 'price', 0 );
		$update = array(
			'id' => $id,
			'brokerage' => $brokerage,
			'price' => $price,
			'img_url' => $img_url,
			'mark' => $mark,
			'status' => SYN_GOODS,
			'upate_time' => date( 'Y-m-d H:i:s' )
		);
		if( $t_url != '' )
		{
			$update['url'] = $t_url;
		}
		$ret_u = $this->Share_goods->save($update);
		if( $ret_u !== true )
		{
			$ret = 1;
			$msg = '更新失败';
		}
		echo json_encode( array( 'ret' => $ret, 'msg' => $msg ) );
    	exit;
    }
    function get_goods()
    {
    	$this->layout = 'empty';
    	$user_id = $_SESSION['user_id'];
    	$result = array();
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$where = array( 'type' => 0,'status' => SYN_GOODS );
    	$ret = $this->Share_goods->findAll( 'type=0 and (status='.SYN_GOODS.' or status='.DOING_GOODS." and auditor=$user_id )", null, null, 1 );
    	if( count( $ret ) > 0 )
    	{
    		$result = $ret[0]['Share_goods'];
    		$update_data = array( 
    			'id' => $ret[0]['Share_goods']['id'],
    			'status' => DOING_GOODS,//审核中
    			'auditor' => $user_id,
    			'update_time' => $curr_time
    		);
    		$this->Share_goods->save( $update_data );
    	}
    	$this->set( 'result', $result );
    	$this->render( 'goods_index' );
    }
    function get_collocation()
    {
    	$this->layout = 'empty';
    	$user_id = $_SESSION['user_id'];
    	$result = array();
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$where = array( 'status' => NEW_COLL );
    	//$order = array( 'create_time' => 'asc' );
    	$field = array( 'id','user_id','col_id','col_name','images','goods_ids','describe','create_time' );
    	$ret = $this->Share_collocation->findAll( 'status='.NEW_COLL.' or status='.DOING_COLL.' and auditor='.$user_id, $field, null, 1 );
    	if( count( $ret ) > 0 )
    	{
    		$temp = $ret[0]['Share_collocation'];
    		$update_data = array( 
    			'id' => $ret[0]['Share_collocation']['id'],
    			'status' => DOING_COLL,//审核中
    			'auditor' => $user_id,
    			'update_time' => $curr_time
    		);
    		$imgs = explode( ',', trim( $temp['images'], ',' ) );
    		foreach ( $imgs as $key => $val )
    		{
    			$imgs[$key] = $this->webroot.'collocation/'.$val;
    		}
    		$result['col'] = $temp;
    		$result['col']['images'] = $imgs;
    		$this->Share_collocation->save( $update_data );
    		$goods_ids = trim( $temp['goods_ids'], ',' );
    		$goods_ids = str_replace( ',', "','", $goods_ids );
    		if( strlen( $goods_ids ) > 0 )
    		{
    			$field_goods = array( 'id','goods_id','goods_name','url','describe','status','brokerage','price','create_time' );
    			$ret_goods = $this->Share_goods->findAll( "goods_id in( '$goods_ids' ) and type=1", $field_goods );
    			foreach ( $ret_goods as $val )
    			{
    				$val = $val['Share_goods'];
    				if( $val['status'] == NEW_GOODS )//当前商品还没有同步淘宝信息，没有判断依据，故当前相关搭配无法审核。
    				{
    					$result = array();
    					$update_data['status'] = NEW_COLL;//将当前搭配状态修改为初始状态。
    					$this->Share_collocation->save( $update_data );
    					break;
    				}
    				$result['goods'][] = $val;
    			}
    		}
    	}
    	$this->set( 'result', $result );
    	$this->render( 'collocation_index' );
    }
    /*	1、未通过的，记下标记，质检后直接删除（连并对应的share_goods一并删除）
	 * 	2、审核通过，但未被推荐，则保留
	 * 	3、审核通过，并被推荐，则copy一份至collocation
	 */
    function audit_collocation()
    {	
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$user_id = $_SESSION['user_id'];
    	$id = init_web_para( 'id', 0 );
    	$ret = 0;
    	$msg = '';
    	$pub_col_flag = init_web_para( 'pub_col_flag', 0 );
    	$operate = init_web_para( 'operate', '' );//ok,通过审核，no,审核不通过
    	$class_one = init_web_para( 'class_one_col', '' );
    	$class_two = init_web_para( 'class_two_col', '' );
    	$class_three = init_web_para( 'class_three_col', '' );
    	$class_four = init_web_para( 'class_four_col', '' );
    	$ret_share_col = $this->Share_collocation->findAll( array( 'id' => $id ),null,null,1 );
    	if( count( $ret_share_col ) )
    	{
    		$temp_s_col = $ret_share_col[0]['Share_collocation'];
    		if( in_array( $temp_s_col['status'], array( NEW_COLL, DOING_COLL ) ) )
    		{
    			$update_data = array(
    				'id' => $id,
    				'auditor' => $user_id,
    				'update_time' => $curr_time
    			);
    			if( $operate == 'ok' || $operate == 'no' )
    			{
    				if( $operate == 'ok' )//通过审核
    				{
    					$update_data['status'] = AUDIT_COLL;
    					$update_data['pub_status'] = $pub_col_flag;
    					if( $pub_col_flag == 1 )//被推荐，需在collocation表中增加一条记录
    					{
    						if( check_class_path( $class_one, $class_two, $class_three, $class_four ) == true )
    						{
    							$insert_date = 	$temp_s_col;
	    						$insert_date['id'] = null;
	    						$insert_date['status'] = 0;
	    						$insert_date['class_one'] = $class_one;
	    						$insert_date['class_two'] = $class_two;
	    						$insert_date['class_three'] = $class_three;
	    						$insert_date['class_four'] = $class_four;
	    						$insert_date['create_time'] = $curr_time;
	    						$insert_date['update_time'] = $curr_time;
	    						unset( $insert_date['pub_status'] );
	    						unset( $insert_date['auditor'] );
	    						$this->Share_collocation->save( $update_data );
	    						$this->Collocation->save($insert_date);
	    						$this->com_ctl->set_score( $temp_s_col['user_id'], TYPE_PUB_COL_SCORE );//分享得分设置
	    						$content = '恭喜您，您分享的搭配（'.$temp_s_col['col_name'].'）审核已通过，并已被推荐！';
	    						$this->send_letter($content, $temp_s_col['user_id'] );
    						}
    						else //归类异常
    						{
    							$ret = 1;
    							$msg = '无效归类，推荐无效';
    						}
    					}
    					else 
    					{
    						$this->Share_collocation->save( $update_data );
    						$this->com_ctl->set_score( $temp_s_col['user_id'], TYPE_COL_SCORE );
    						$content = '恭喜您，您分享的搭配（'.$temp_s_col['col_name'].'）审核已通过！';
	    					$this->send_letter($content, $temp_s_col['user_id'] );
    					}
    					$ret_array = $this->audit_goods( 1, $operate );//批量审核改搭配相关的商品
    					if( $ret_array['ret'] != 0 )
    					{
    						$msg = $ret_array['msg'];
    					}
    				}
    				else ////未通过审核
    				{
    					$update_data['status'] = FAIL_COLL;
    					$this->Share_collocation->save( $update_data );
    					$ret_array = $this->audit_goods( 1, $operate );
	    				$content = '很遗憾，您分享的搭配（'.$temp_s_col['col_name'].'）审核未通过，原因如下：<br>'.filter_content( init_web_para( 'content', '' ) );
	    				$this->send_letter($content, $temp_s_col['user_id'] );
    				}  
    									
    			}
    			else //无效操作
    			{
    				$ret = 1;
    				$msg = '无效操作';
    				$this->Share_collocation->save( array( 'id' => $id,'status' => NEW_COLL ) );//还原状态
    			}
    		}
	    	else //该搭配已经被其他人审核过了
	    	{
	    		$ret = 1;
	    		$msg = '该搭配已被他人审核';
	    	}
    	}
    	else
    	{
    		$ret = 1;
    		$msg = '无对应的待审搭配';
    	}
    	echo json_encode( array( 'ret' => $ret, 'msg' => $msg ) );
    	exit;
    }
    function audit_goods( $type=0, $col_operate='0k' )
    {
    	$ret = 0;
    	$msg = '';
    	$user_id = $_SESSION['user_id'];
    	$curr_time = date( 'Y-m-d H:i:s' );
    	if( $type == 0 )//普通上传商品，每次只会审核一个商品
    	{
    		$operate = init_web_para( 'operate', '' );//ok,通过审核，no,审核不通过
    		$pub_goods_flag = init_web_para( 'pub_goods_flag', 0 );
    		$id = init_web_para( 'id', '' );
    		$class_one = init_web_para( 'class_one', '' );
    		$class_two = init_web_para( 'class_two', '' );
    		$class_three = init_web_para( 'class_three', '' );
    		$class_four = init_web_para( 'class_four', '' );
    		$ret_share_goods = $this->Share_goods->findAll( array( 'id' => $id ),null,null,1 );
    		if( count( $ret_share_goods ) > 0 )
    		{
    			$temp_s_goods = $ret_share_goods[0]['Share_goods'];
    			if( in_array( $temp_s_goods['status'], array( SYN_GOODS, DOING_GOODS ) ) )//状态为正坐审核或初始状态
    			{
    				$update_data = array(
	    				'id' => $id,
	    				'auditor' => $user_id,
	    				'update_time' => $curr_time
	    			);
	    			if( $operate == 'ok' || $operate == 'no' )
	    			{
	    				if( $operate == 'ok' )//通过审核
	    				{
	    					$update_data['status'] = AUDIT_GOODS;
	    					if( $pub_goods_flag == 1 )//被推荐，需在goods表中增加一条记录
	    					{
	    						if( check_class_path( $class_one, $class_two, $class_three, $class_four ) == true )
	    						{
	    							$ret_put_goods = $this->Goods->findAll( array( 'mark' => $temp_s_goods['mark'] ), null, null, 1 );
	    							if( count( $ret_put_goods ) > 0 )//同一个商品已经被推荐过
	    							{
		    							$this->com_ctl->set_score( $temp_s_goods['user_id'], TYPE_GOODS_SCORE );//分享得分设置
		    							$content = '恭喜您，您分享的商品（'.$temp_s_goods['goods_name'].'）审核已通过！';
		    							$this->send_letter($content, $temp_s_goods['user_id'] );
	    							}
	    							else 
	    							{
	    								$update_data['pub_status'] = 1;
	    								$insert_date = $temp_s_goods;
			    						$insert_date['id'] = null;
			    						$insert_date['status'] = 0;
			    						$insert_date['class_one'] = $class_one;
			    						$insert_date['class_two'] = $class_two;
			    						$insert_date['class_three'] = $class_three;
			    						$insert_date['class_four'] = $class_four;
			    						$insert_date['create_time'] = $curr_time;
			    						$insert_date['update_time'] = $curr_time;
			    						unset( $insert_date['pub_status'] );
			    						unset( $insert_date['auditor'] );
			    						$this->Goods->save( $insert_date );//copy一份数据到goods表
			    						$this->com_ctl->set_score( $temp_s_goods['user_id'], TYPE_PUB_GOODS_SCORE );//分享得分设置
		    							$content = '恭喜您，您分享的商品（'.$temp_s_goods['goods_name'].'）审核已通过，并已被推荐！';
		    							$this->send_letter($content, $temp_s_goods['user_id'] );
	    							}
	    							$this->Share_goods->save( $update_data );
	    						}
	    						else //归类异常
	    						{
	    							$ret = 1;
	    							$msg = '无效归类，推荐无效';
	    						}
	    					}
	    					else 
	    					{
	    						$this->Share_goods->save( $update_data );
	    						$this->com_ctl->set_score( $temp_s_goods['user_id'], TYPE_GOODS_SCORE );
    							$content = '恭喜您，您分享的商品（'.$temp_s_goods['goods_name'].'）审核已通过！';
	    						$this->send_letter($content, $temp_s_goods['user_id'] );
	    					}
	    				}
	    				else ////未通过审核
	    				{
	    					$update_data['status'] = FAIL_GOODS;
	    					$this->Share_goods->save( $update_data );
	    					$content = '很遗憾，您分享的商品（'.$temp_s_goods['goods_name'].'）审核未通过，原因如下：<br>'.filter_content( init_web_para( 'content', '' ) );
	    					$this->send_letter($content, $temp_s_goods['user_id'] );
	    				}				
	    			}
	    			else //无效操作
	    			{
	    				$ret = 1;
	    				$msg = '无效操作';
	    				$this->Share_goods->save( array( 'id' => $id,'status' => NEW_GOODS ) );//还原状态
	    			}
    			}
    			else//该搭配已经被其他人审核过了
		    	{
		    		$ret = 1;
		    		$msg = '该搭配已被他人审核';
		    	}
    		}
    		else
	    	{
	    		$ret = 1;
	    		$msg = '无对应的待审商品';
	    	}
    		echo json_encode( array( 'ret' => $ret, 'msg' => $msg ) );
    		exit;
    	}
    	else//搭配的商品，一个搭配可能有多个商品
    	{
    		
    		$pub_goods_flag = init_web_para( 'pub_goods_flag', array() );
    		$goods_ids = init_web_para( 'goods_ids', array() );
    		$counts = count( $goods_ids );
    		$class_one = init_web_para( 'class_one', array() );
    		$class_two = init_web_para( 'class_two', array() );
    		$class_three = init_web_para( 'class_three', array() );
    		$class_four = init_web_para( 'class_four', array() );
   			for ( $i = 0; $i < $counts; $i++ )
    		{
    			$id = $goods_ids[$i];
    			$ret_goods = $this->Share_goods->findAll( array( 'id' => $id, 'type' => 1 ),null,null,1 );
    			if( count( $ret_goods ) > 0 )
    			{
    				$temp_goods = $ret_goods[0]['Share_goods'];
		    		if( in_array( $temp_goods['status'], array( SYN_GOODS, DOING_GOODS ) ) )//
		    		{
		    			if( $col_operate == 'ok' )//如果相关的搭配审核通过，则所属商品全部通过
		    			{
			    			$update_date = array(
			    				'id' => $id,
			    				'status' => AUDIT_GOODS,
				    			'auditor' => $user_id,
			    				'create_time' => $curr_time,
			    				'update_time' => $curr_time
			    			);
			    			if( $pub_goods_flag[$i] == 1 )//当前商品被推荐
			    			{
			    				if( check_class_path( $class_one[$i], $class_two[$i], $class_three[$i], $class_four[$i] ) == true )
	    						{
	    							$ret_put_goods = $this->Goods->findAll( array( 'mark' => $temp_goods['mark'] ), null, null, 1 );
	    							if( count( $ret_put_goods ) > 0 )//同一个商品已经被推荐过
	    							{
	    								$this->com_ctl->set_score( $temp_goods['user_id'], TYPE_GOODS_SCORE );
	    								$content = '恭喜您，您分享的商品（'.$temp_goods['goods_name'].'）审核已通过！';
		    							$this->send_letter($content, $temp_goods['user_id'] );
	    							}
	    							else 
	    							{
	    								$update_date['pub_status'] = 1;
	    								$insert_data = $temp_goods;
					    				$insert_date['id'] = null;
			    						$insert_date['status'] = 0;
			    						$insert_date['class_one'] = $class_one[$i];
			    						$insert_date['class_two'] = $class_two[$i];
			    						$insert_date['class_three'] = $class_three[$i];
			    						$insert_date['class_four'] = $class_four[$i];
			    						$insert_date['create_time'] = $curr_time;
			    						$insert_date['update_time'] = $curr_time;
			    						unset( $insert_date['pub_status'] );
			    						unset( $insert_date['auditor'] );
			    						$this->Goods->save( $insert_date );//copy一份数据到goods表
			    						unset( $insert_date );
			    						$this->com_ctl->set_score( $temp_goods['user_id'], TYPE_PUB_GOODS_SCORE );
	    								$content = '恭喜您，您分享的商品（'.$temp_goods['goods_name'].'）审核已通过，并已被推荐！';
		    							$this->send_letter($content, $temp_goods['user_id'] );
	    							}
	    							$this->Share_goods->save( $update_date );
	    						}
	    						else //归类异常
	    						{
	    							$ret = 1;
	    							$msg .= "当前商品(id=$id)无效归类，推荐无效";
	    						}
			    			}
			    			else
			    			{
			    				$this->Share_goods->save( $update_date );
			    				$this->com_ctl->set_score( $temp_goods['user_id'], TYPE_GOODS_SCORE );
			    			}
		    			}
		    			else //如果相关的搭配审核未通过，则所属商品全部不通过
		    			{
		    				$update_date = array(
			    				'id' => $id,
			    				'status' => FAIL_GOODS,
				    			'auditor' => $user_id,
			    				'create_time' => $curr_time,
			    				'update_time' => $curr_time
			    			);
		    				$this->Share_goods->save( $update_date );
		    			}
		    			
			    		unset( $update_date );
		    		}
		    		else 
		    		{
		    			$ret = 1;
	    				$msg .= "当前商品(id=$id)已被他人审核";
		    		}
		    		unset( $temp_goods );
    			}
    			else 
    			{
    				$ret = 1;
    				$msg .= "没有id为$id的商品";
    			}
    			unset( $ret_goods );
    		}
    		return array( 'ret' => $ret, 'msg' => $msg );
    		exit;
    	}
    }
    function send_letter( $content,$to_user_id )
    {
    	$content = filter_content( $content );
    	$curr_time = date( 'Y-m-d H:i:s' );
    	$from_user_id = $_SESSION['user_id'];
    	$ret_con = $this->Contacter->findAll( "from_user_id=$from_user_id and to_user_id=$to_user_id or from_user_id=$to_user_id and to_user_id=$from_user_id", 
    				null, null, 1 );
    	$contact_data = array();
    	$letter_data = array();
    	if( isset( $ret_con[0]['Contacter']['contact_id'] ) )
    	{
    		$contact_id = $ret_con[0]['Contacter']['contact_id'];
    		$contact_data['id'] = $ret_con[0]['Contacter']['id'];
    		$contact_data['update_time'] = date( 'Y-m-d H:i:s' );
    	}
    	else 
    	{
    		$contact_id = get_contact_id();
    		$contact_data['id'] = null;
    		$contact_data['contact_id'] = $contact_id;
    		$contact_data['from_user_id'] = $from_user_id;
    		$contact_data['to_user_id'] = $to_user_id;
    		$contact_data['update_time'] = $curr_time;
    		$contact_data['create_time'] = $curr_time;
    	}
    	$this->Contacter->save( $contact_data );
    	$letter_data['id'] = null;
    	$letter_data['contact_id'] = $contact_id;
    	$letter_data['from_user_id'] = $from_user_id;
    	$letter_data['to_user_id'] = $to_user_id;
    	$letter_data['content'] = $content;
    	$letter_data['create_time'] = $curr_time;
		$this->Letter->save( $letter_data );
		return true;
    }
    function get_class( $parent_code, $depth, $type=0, $status = 1 )
    {
    	$ret = $this->com_ctl->get_class_path($parent_code, $depth,$type,$status);
    	echo json_encode( array( 'ret' => 0, 'data'=> $ret ) );
    	exit;
    }
    function class_path()
    {
    	$this->set( 'audit_type', 5 );
    }
    function get_class_info()
    {
    	$this->layout='empty';
    	$type = init_web_para( 'type', 0 );
    	$first_code = init_web_para( 'first_code', '' );
    	$sec_code = init_web_para( 'sec_code', '' );
    	$three_code = init_web_para( 'three_code', '' );
    	$status = init_web_para( 'status', 1 );
    	$ret = array();
    	$result = array();
    	$where_status = '';
    	if( $status == 1 ) 
    	{
    		$where_status = ' and status=1';
    	}
    	if( $three_code != '' )
    	{
    		$ret = $this->Class_path->findAll( "parent_code=$three_code and depth=3$where_status" );
    	}
    	elseif( $sec_code != '' )
    	{
    		$ret = $this->Class_path->findAll( "parent_code=$sec_code and depth=2$where_status" );
    	}
    	elseif( $first_code != '' )
    	{
    		$ret = $this->Class_path->findAll( "parent_code=$first_code and depth=1$where_status" );
    	}
    	else 
    	{
    		$ret = $this->Class_path->findAll( "parent_code=0 and depth=0$where_status" );
    	}
    	foreach ( $ret as $val )
    	{
    		$val = $val['Class_path'];
    		$result[] = $val;
    	}
    	$this->set( 'result', $result );
    }
    function set_class()
    {
    	$type = init_web_para( 'type', 0 );
    	$status = init_web_para( 'status', 1 );
    	$depth = init_web_para( 'depth', -1 );
    	$code = init_web_para( 'code', -1 );
    	$name = init_web_para( 'name', '' );
    	$parent_code = init_web_para( 'parent_code', 0 );
    	$id = null;
    	if( $code != -1 )
    	{
    		$ret = $this->Class_path->findAll( "code=$code and type=$type" );
    		$id = isset( $ret[0]['Class_path']['id'] )?$ret[0]['Class_path']['id']:null;
    	}
    	if( $id != null )
    	{
	    	$update = array(
	    		'id' => $id,
	    		'status' => $status,
	    		'name' => $name,
	    		'type' => $type,
	    		'operator' => $_SESSION['user_id'],
	    		'update_time' => date( 'Y-m-d H:i:s' )
	    	);
    	}
    	else 
    	{
    		$ret = $this->Class_path->findAll( "depth=$depth and type=$type", null, 'code desc', 1 );
    		$code = isset( $ret[0]['Class_path']['code'] )?$ret[0]['Class_path']['code']:0;
    		$code++;
    		$update = array(
	    		'id' => null,
	    		'status' => $status,
	    		'depth' => $depth,
	    		'type' => $type,
    			'name' => $name,
    			'code' => $code,
    			'operator' => $_SESSION['user_id'],
	    		'parent_code' =>$parent_code,
    			'create_time' => date( 'Y-m-d H:i:s' ),
    			'update_time' => date( 'Y-m-d H:i:s' )
	    	);
    	}
    	$this->Class_path->save( $update );
    	exit;
    }
}